#!/usr/bin/python

import BeautifulSoup
import urllib2
import time
import sys

def get_buyback_price(isbn):
    url = "http://text.uwbookstore.com/BuybackBooks.aspx?i=" + str(isbn)
    page = urllib2.urlopen(url)
    text = page.read()
    page.close()

    if text.count("Buyback Price") > 0:
        i = text.index("Buyback Price:")
        priceline = text[i:i+100]
        a = priceline.index("$")
        b = priceline.index(".")
        return float(priceline[a+1:b+3])
    else:
        return None

for l in sys.stdin:
    isbn = l.strip()
    print '%s\t%s' % (isbn, str(get_buyback_price(isbn[:-1])))

#isbns = []
#if len(sys.argv) > 1:
#    for k in range(1, len(sys.argv)):
#        isbns.append(sys.argv[k])
#    for i in isbns:
#        #time.sleep(0.5)
#        print i, "\t", get_buyback_price(i[:-1])

'''
Sample List
9781455704187 9780393975291 9780345345059 9780070542358 9780070542358 9780387946146 9780131816299 9780471433347 9780133198317 9780321797056 9780132296540 9780130084514 9780914098911
'''
